package com.ruix.app.ui.adapter;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ruix.app.R;
import com.ruix.app.model.Course;
import com.ruix.app.model.Lesson;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by hadexs on 2017/5/9.
 */

public class CourseListAdapter extends BaseExpandableListAdapter {

    private Context mContext;

    private List<Course> courses;
    private boolean isDesc;
    private SortListener listener;
    private String courseInfo;

    public CourseListAdapter(Context mContext) {
        this.mContext = mContext;
    }

    public void setCourses(List<Course> courses) {
        this.courses = courses;
        notifyDataSetChanged();
    }

    public void setCourseInfo(String courseInfo) {
        this.courseInfo = courseInfo;
    }

    public void addCourses(List<Course> coursesList) {
        if (courses == null) {
            courses = new ArrayList<>();
        }
        this.courses.addAll(coursesList);
        notifyDataSetChanged();
    }


    @Override
    public int getGroupCount() {
        if (courses != null) {
            return courses.size();
        }
        return 0;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        if (courses != null) {
            return courses.get(groupPosition).lessonsSize();
        }
        return 0;
    }

    @Override
    public Course getGroup(int groupPosition) {
        if (courses != null) {
            return courses.get(groupPosition);
        }
        return null;
    }

    @Override
    public Lesson getChild(int groupPosition, int childPosition) {
        if (courses != null) {
            return courses.get(groupPosition).getLessons().get(childPosition);
        }
        return null;
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        ViewSeparatorHolder viewHolder;
        if (convertView == null) {
            viewHolder = new ViewSeparatorHolder();
            convertView = View.inflate(mContext, R.layout.item_course_separator, null);
            viewHolder.courseContain = (RelativeLayout) convertView.findViewById(R.id.ll_course_contain);
            viewHolder.topContain = (RelativeLayout) convertView.findViewById(R.id.ll_top_commtain);
            viewHolder.courseTitle = (TextView) convertView.findViewById(R.id.tv_course_title);
            viewHolder.sortBtn = (TextView) convertView.findViewById(R.id.btn_sort_list);
            viewHolder.courseNum = (TextView) convertView.findViewById(R.id.tv_course_num);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewSeparatorHolder) convertView.getTag();
        }
       Course course= getGroup(groupPosition);
        if (course != null) {
            if (groupPosition == 0){
                if (isDesc){
                    viewHolder.sortBtn.setText("倒序");
                } else {
                    viewHolder.sortBtn.setText("升序");
                }
                if (courseInfo != null) {
                    viewHolder.courseNum.setText(courseInfo);
                }

                viewHolder.topContain.setVisibility(View.VISIBLE);
                viewHolder.sortBtn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (listener != null){
                            listener.sortCourse(isDesc);
                        }
                    }
                });
            } else {
                viewHolder.topContain.setVisibility(View.GONE);
            }
            viewHolder.courseTitle.setText(course.getName());
            GradientDrawable gradientDrawable = (GradientDrawable) viewHolder.courseContain.getBackground();
            if (!TextUtils.isEmpty(course.getColor())) {
                if (course.getColor().length() == 7 || course.getColor().length() == 9 ){
                    gradientDrawable.setColor(Color.parseColor(course.getColor()));
                }
            }
        }
        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ViewSubHolder viewHolder;
        if (convertView == null) {
            viewHolder = new ViewSubHolder();
            convertView = View.inflate(mContext, R.layout.item_course_subitem, null);
            viewHolder.courseSortNum = (TextView) convertView.findViewById(R.id.tv_course_sort);
            viewHolder.courseNmae = (TextView) convertView.findViewById(R.id.tv_course_name);
            viewHolder.courseStudenNum = (TextView) convertView.findViewById(R.id.tv_course_student_num);
            viewHolder.courseExpireTime = (TextView) convertView.findViewById(R.id.tv_course_expire_time);
            viewHolder.statusIcon = (ImageView) convertView.findViewById(R.id.iv_finish_tag);
            viewHolder.dividerView =  convertView.findViewById(R.id.divider_line_view);

            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewSubHolder) convertView.getTag();
        }
        viewHolder.pudateView(mContext, getChild(groupPosition, childPosition),getGroup(groupPosition).isLastLesson(groupPosition));
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }


    static class ViewSeparatorHolder {

        RelativeLayout courseContain;
        RelativeLayout topContain;
        TextView sortBtn;
        TextView courseNum;
        TextView courseTitle;

        public void pudateView(Course course, int groupPosition, final boolean isDesc) {
            if (course != null) {
                if (groupPosition == 0){
                    if (isDesc){
                        sortBtn.setText("升序");
                    } else {
                        sortBtn.setText("倒叙");
                    }
                    topContain.setVisibility(View.VISIBLE);
                } else {
                    topContain.setVisibility(View.GONE);
                }


                courseTitle.setText(course.getName());
//                course.getColor();
//                GradientDrawable gradientDrawable = (GradientDrawable) courseContain.getBackground();
//                gradientDrawable.setColor(Color.parseColor(course.getColor()));
            }

        }



    }

    static class ViewSubHolder {
        TextView courseSortNum;
        TextView courseStudenNum;
        TextView courseNmae;
        TextView courseExpireTime;
        ImageView statusIcon;
        View dividerView;

        public void pudateView(Context context, Lesson lesson,boolean isLastLesson) {
            if (lesson != null) {
                if (lesson.isLearnt()) {
                    statusIcon.setVisibility(View.VISIBLE);
                } else {
                    statusIcon.setVisibility(View.GONE);
                }
                courseSortNum.setText(String.valueOf(lesson.getSort()));
                courseNmae.setText(lesson.getName());
                String studentNumValue = String.format("已有%d人学习", lesson.getStudentNum());
                courseStudenNum.setText(studentNumValue);

                if (lesson.getDiscussTime() > 0) {
                    SimpleDateFormat format = new SimpleDateFormat("MM月dd日 HH:mm");
                    Date d1 = new Date(lesson.getDiscussTime());
                    String disussTime = format.format(d1);
                    String displayDisuss = String.format("共修时间 %s", disussTime);
                    courseExpireTime.setText(displayDisuss);

                    if (System.currentTimeMillis() > lesson.getDiscussTime()) {
                        courseExpireTime.setTextColor(context.getResources().getColor(R.color.color_99));
                    } else {
                        courseExpireTime.setTextColor(context.getResources().getColor(R.color.green));
                    }
                } else {
                    courseExpireTime.setText("");
                }

                if (isLastLesson){
                    dividerView.setVisibility(View.INVISIBLE);
                } else {
                    dividerView.setVisibility(View.VISIBLE);
                }
            }
        }
    }

    public void setDesc(boolean desc) {
        isDesc = desc;
    }

    public boolean isDesc() {
        return isDesc;
    }

    public void setListener(SortListener listener) {
        this.listener = listener;
    }

    public interface  SortListener{
        void sortCourse(boolean desc);
    }
}
